package containerlogistik.implementation;

import java.util.List;
import containerlogistik.interfaces.Length;
import containerlogistik.interfaces.Track;
import containerlogistik.interfaces.TrackSection;

public class TrackImpl implements Track {

    private String name;
    private Length length;
    private String backgroundPicture;
    private List<TrackSection> sections;
    
    private TrackImpl(String name, List sections, String backgroundPicture) {
        this.name = name;
        this.sections = sections;
        this.backgroundPicture = backgroundPicture;
        this.length = this.sumLength();
    }

    static Track valueOf(String name, List sections) {
        return new TrackImpl(name, sections, null);
    }

    static Track valueOf(String name, List sections, String backgroundPicture) {
        return new TrackImpl(name, sections, backgroundPicture);
    }

    private Length sumLength() {
        Length sumLength = Values.lengthInM(0);
        for (TrackSection trackPart : this.sections()) {
            sumLength = sumLength.add(trackPart.length());
        }
        return sumLength;
    }

    public String name() {
        return this.name;
    }

    public Length length() {
        return this.length;
    }
    
    public String backgroundPicture() {
        return this.backgroundPicture;
    }

    public List<TrackSection> sections() {
        return this.sections;
    }
}